#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <queue>
using namespace std;
const int N = 50005;
int n, m, head[N], cnt, dfn[N], low[N], tim, Q[N], top, sum, scc[N], du[N],
    num[N], tot, ans, in[N], p[N], f[N], len, pos, val[N], w[N], hed[N];
bool vis[N];
struct Node {
  int u, v, nxt;
} edge[N << 1];
struct node {
  int u, v, nxt;
} g[N];
void add(int u, int v) {
  edge[++cnt] = (Node){u, v, head[u]};
  head[u] = cnt;
}
void ad(int u, int v) {
  g[++pos] = (node){u, v, hed[u]};
  hed[u] = pos;
}
void tarjan(int u) {
  dfn[u] = low[u] = ++tim;
  vis[u] = 1;
  Q[++top] = u;
  for (int i = head[u]; i; i = edge[i].nxt) {
    int v = edge[i].v;
    if (dfn[v] == -1) {
      tarjan(v);
      low[u] = min(low[u], low[v]);
    } else if (vis[v]) {
      low[u] = min(low[u], dfn[v]);
    }
  }
  if (dfn[u] == low[u]) {
    ++sum;
    int now;
    do {
      now = Q[top];
      top--;
      scc[now] = sum;
      vis[now] = 0;
      num[sum]++;
    } while (now != u);
  }
}
void topo() {
  queue<int> q;
  for (int i = 1; i <= sum; i++) {
    if (in[i] == 0) {
      q.push(i);
    }
  }
  while (!q.empty()) {
    int u = q.front();
    q.pop();
    p[++len] = u;
    for (int i = hed[u]; i; i = g[i].nxt) {
      int v = g[i].v;
      in[v]--;
      if (in[v] == 0) {
        q.push(v);
      }
    }
  }
}
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &w[i]);
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    scanf("%d%d", &x, &y);
    add(x, y);
  }
  memset(dfn, -1, sizeof(dfn));
  for (int i = 1; i <= n; i++) {
    if (dfn[i] == -1) {
      tarjan(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = head[i]; j; j = edge[j].nxt) {
      if (scc[edge[j].u] != scc[edge[j].v]) {
        in[scc[edge[j].v]]++;
        ad(scc[edge[j].u], scc[edge[j].v]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    val[scc[i]] += w[i];
  }
  for (int i = 1; i <= sum; i++) {
    f[i] = val[i];
  }
  topo();
  for (int i = 1; i <= len; i++) {
    for (int j = hed[p[i]]; j; j = g[j].nxt) {
      int u = g[j].u, v = g[j].v;
      f[v] = max(f[v], f[u] + val[v]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= len; i++) {
    ans = max(ans, f[i]);
  }
  printf("%d\n", ans);
  return 0;
}
